//
//  SCDynamicSegmentView.m
//  Shuaiche
//
//  Created by zzs on 2018/10/12.
//  Copyright © 2018年 zzs. All rights reserved.
//

#import "SCDynamicSegmentView.h"
#import <Masonry/Masonry.h>

@interface _SCDynamicSegmentViewCell : UICollectionViewCell
@property (nonatomic, strong) UILabel *textLabel;
@property (nonatomic, strong) UIView *indicatorLine;
@property (nonatomic, assign) float itemWidth;
@end

@implementation _SCDynamicSegmentViewCell

- (UILabel *)textLabel {
    if (!_textLabel) {
        UILabel *label = [[UILabel alloc] init];
        label.font = SC_FONT(15);
        label.textColor = SC_TextBlack_Third;
        label.textAlignment = 1;
        [self.contentView addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
          make.edges.mas_equalTo(0);
          make.height.mas_equalTo(44);
        }];
        _textLabel = label;
    }
    return _textLabel;
}

- (UICollectionViewLayoutAttributes *)preferredLayoutAttributesFittingAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes{
    
    UICollectionViewLayoutAttributes *attributes = [super preferredLayoutAttributesFittingAttributes:layoutAttributes];
    CGSize size = [self.textLabel.text sizeWithAttributes:@{NSFontAttributeName: self.textLabel.font}];
    CGRect frame = attributes.frame;
    if (_itemWidth) {
        frame.size.width = _itemWidth;
    }else{
        frame.size.width = ceilf(size.width) + 10;
    }
    attributes.frame = frame;
    return attributes;
}

- (UIView *)indicatorLine {
    if (!_indicatorLine) {
        _indicatorLine = [[UIView alloc] init];
        _indicatorLine.backgroundColor = SC_Color_Tint;
        [self.contentView addSubview:_indicatorLine];
        _indicatorLine.translatesAutoresizingMaskIntoConstraints = NO;
        [_indicatorLine.widthAnchor constraintEqualToConstant:30].active = YES;
        [_indicatorLine.centerXAnchor constraintEqualToAnchor:self.contentView.centerXAnchor].active = YES;
        [_indicatorLine.bottomAnchor constraintEqualToAnchor:self.contentView.bottomAnchor].active = YES;
        [_indicatorLine.heightAnchor constraintEqualToConstant:3].active = YES;
    }
    return _indicatorLine;
}

- (void)setSelected:(BOOL)selected {
    [super setSelected:selected];
    if (selected) {
        self.textLabel.font = SC_FONT_Meduim(15);
        self.textLabel.textColor = SC_TextBlack_Primary;
        self.indicatorLine.hidden = NO;
    } else {
        self.textLabel.font = SC_FONT(15);
        self.textLabel.textColor = SC_TextBlack_Third;
        _indicatorLine.hidden = YES;
    }
}

@end

@interface SCDynamicSegmentView()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UIView *line;
@end

@implementation SCDynamicSegmentView

- (UIView *)line {
    if (!_line) {
        _line = [[UIView alloc] init];
        _line.backgroundColor = SC_RGB(220, 220, 220);
        [self addSubview:_line];
    }
    return _line;
}

- (void)layoutSubviews {
    _collectionView.frame = self.bounds;
    if (!self.hiddenBottomLine) {
        self.line.frame = CGRectMake(0, CGRectGetHeight(self.bounds) - 1, CGRectGetWidth(self.bounds), 0.5);
    }
}

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout.minimumInteritemSpacing = 5;
//        layout.estimatedItemSize = CGSizeMake(100, CGRectGetHeight(self.bounds));
        _collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:layout];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.showsHorizontalScrollIndicator = NO;
//        _collectionView.contentInset = UIEdgeInsetsMake(0, 0, 0, 20);
        [_collectionView registerClass:_SCDynamicSegmentViewCell.class forCellWithReuseIdentifier:@"cell"];
        [self addSubview:_collectionView];
    }
    return _collectionView;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _titles.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    _SCDynamicSegmentViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.itemWidth = _itemWidth;
    cell.textLabel.text = _titles[indexPath.item];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

    NSString *title = _titles[indexPath.row];
    CGSize size = [title sizeWithAttributes:@{NSFontAttributeName: SC_FONT(15)}];
    CGFloat width = 0;
    if (_itemWidth) {
        width = _itemWidth;
    }else{
        width = ceilf(size.width) + 10;
    }
    return CGSizeMake(width, 44);

}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if([self.delegate respondsToSelector:@selector(segmentDidClickedAtIndex:)]){
        [self.delegate segmentDidClickedAtIndex:indexPath.item];
    }
    
    [self selectedAtIndexPath:indexPath];
}

- (void)setTitles:(NSArray *)titles{
    if (_titles) {
        _collectionView.dataSource = nil;
        _titles = titles;
        _collectionView.dataSource = self;
        [self.collectionView reloadData];
    } else {
        _titles = titles;
        [self.collectionView reloadData];
    }
}

- (void)selectedAtIndex:(NSInteger)index {
    [self selectedAtIndexPath:[NSIndexPath indexPathForItem:index inSection:0]];
}

- (void)selectedAtIndexPath:(NSIndexPath *)indexPath {
    [_collectionView selectItemAtIndexPath:indexPath animated:YES scrollPosition:UICollectionViewScrollPositionNone];
    if ([_collectionView cellForItemAtIndexPath:indexPath]) {
        [_collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
    }
}

@end
